package Serializable_package;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Cat implements Serializable
{ 
	private static final long serialVersionUID = 8738236897926701290L;
	private String name;
	private int age;
	
	public Cat(String name, int age)
	{
		this.name = name;
		this.age = age;
	}
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return age;
	}
}
public class SerializableExample
{
	public static void main(String[] args)
	{
		String filePath = "E:/CatTest.txt";
		Cat c1 = new Cat("Pussy1", 1);
		Cat c2 = new Cat("Pussy2", 2);
		Cat c3 = new Cat("Pussy3", 3);
		System.out.println("Before Serialization");
		System.out.println("Name: " + c1.getName() + " and Age: " + c1.getAge());
		
		try 
		{
			
			FileOutputStream fos = new FileOutputStream(filePath);
			ObjectOutputStream os = new ObjectOutputStream(fos);
			
			os.writeObject(c3);
			os.writeObject(c2);
			os.writeObject(c1);
			os.close();
			
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
		try 
		{
			FileInputStream fis =  new FileInputStream(filePath);
			ObjectInputStream is = new ObjectInputStream(fis);
			Cat myCat = (Cat) is.readObject();
			System.out.println("After Serialization");
			System.out.println("Name: " + myCat.getName() + " and Age: " + myCat.getAge());
		} 
		catch (Exception e) 
		{	
			e.printStackTrace();
		}
	}
}
